﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CDSBDemandControl.Presenter;
using CDSBDemandControl.Model;
using CDSBDemandControl.Configuration;
using System.Web.UI.HtmlControls;
using System.Drawing;

namespace CDSBDemandControl.View.Web.PaginasInternas
{
    public partial class Atrasos : System.Web.UI.Page, IDiscriminatorDemand
    {
        private Presenter.DiscriminatorDemand Presenter;

        protected void Page_Load(object sender, EventArgs e)
        {
            Response.Cache.SetExpires(DateTime.Now.Subtract(new TimeSpan(24, 0, 0)));
            Response.Cache.SetCacheability(HttpCacheability.NoCache);

            if (!this.IsPostBack || this.Session["Presenter"] == null)
            {
                this.Presenter = new DiscriminatorDemand(this);

                this.Session["Presenter"] = this.Presenter;

                if (!this.IsPostBack) this.ListarAtrasos();
            }
            else
            {
                this.Presenter = (Presenter.DiscriminatorDemand)this.Session["Presenter"];
                this.Presenter.ResetView(this);
            }
        }

        private void ListarAtrasos()
        {
            this.Presenter.ListWorkItemsOutOfDate();
        }

        public void ExibirAtrasos(IEnumerable<DemandModel> PendingDemands, IEnumerable<DemandModel> Concludeddemands)
        {
            // Binding
            this.GrdAtrasoTasksPendentes.DataSource = PendingDemands;
            this.GrdAtrasoTasksPendentes.DataBind();

            this.GrdAtrasoTasksConcluidas.DataSource = Concludeddemands;
            this.GrdAtrasoTasksConcluidas.DataBind();

            // Exibe número total de registros
            string Texto1 = string.Empty;
            string Texto2 = string.Empty;

            if (GrdAtrasoTasksPendentes.Rows.Count > 0)
            {
                Texto1 = string.Format("Média de {0} dia{2} de atraso em {1} WorkItem{2} em andamento", PendingDemands.GetAverageDelays(), PendingDemands.Count<DemandModel>(), PendingDemands.Count<DemandModel>() > 1 ? "s" : string.Empty);
            }

            if (GrdAtrasoTasksConcluidas.Rows.Count > 0)
            {
                Texto2 = string.Format("Média de {0} dia{2} de atraso em {1} WorkItem{2} concluídos", Concludeddemands.GetAverageDelays(), Concludeddemands.Count<DemandModel>(), Concludeddemands.Count<DemandModel>() > 1 ? "s" : string.Empty);
            }

            this.ExibirTotaisRegistro(Texto2, Texto1);

            this.ExibirObservacao(string.Format("Tasks no período de {0} à {1}.", ConfigManager.GetPeriodStartDate().ToBrazilianShortDateForUSAShortDate(), DateTime.Now.Date.ToShortDateString()));

            Texto1 = GrdAtrasoTasksPendentes.Rows.Count > 0 ? string.Empty : "Não há workItem atrasados e pendentes.";
            Texto2 = GrdAtrasoTasksConcluidas.Rows.Count > 0 ? string.Empty : "Não há workItem entregue com atraso.";

            // Exibe Mensagem quando não houver dados na Grid
            this.ValidarGridSemResultado(Texto1, Texto2);

            // Exibe a data da última atualização
            (this.Master.FindControl("LblUltimoUpdate") as Label).Text = "Última atualização em: " + this.Presenter.DemandListOutOfDateLastUpdate.ToShortDateString() + " as " + this.Presenter.DemandListOutOfDateLastUpdate.ToShortTimeString();

            (this.Master.FindControl("txtTimeToCompare") as HtmlGenericControl).InnerHtml =
                this.Presenter.DemandListOutOfDateLastUpdate.Year + "," +
                (this.Presenter.DemandListOutOfDateLastUpdate.Month - 1) + "," +
                this.Presenter.DemandListOutOfDateLastUpdate.Day + "," +
                this.Presenter.DemandListOutOfDateLastUpdate.Hour + "," +
                this.Presenter.DemandListOutOfDateLastUpdate.Minute + "," +
                this.Presenter.DemandListOutOfDateLastUpdate.Second;
        }

        private void ExibirTotaisRegistro(string texto1, string texto2)
        {
            this.divTotalRegGrd01.Visible = texto1.Length > 0 ? true : false;
            this.lblTotalRegistros1.Text = texto1;

            this.divTotalRegGrd02.Visible = texto2.Length > 0 ? true : false;
            this.lblTotalRegistros2.Text = texto2;
        }

        private void ExibirObservacao(string obs)
        {
            this.LblObsOrdenacao.Text = obs;
            this.LblObsOrdenacao.Visible = this.LblObsOrdenacao.Text.Length > 0 ? true : false;
        }

        private void ValidarGridSemResultado(string msgSemResultado1, string msgSemResultado2)
        {
            this.DivMsgSemResultados1.Visible = (msgSemResultado1.Length > 0);
            this.lblMsgSemResultados1.Text = msgSemResultado1;

            this.DivMsgSemResultados2.Visible = (msgSemResultado2.Length > 0);
            this.lblMsgSemResultados2.Text = msgSemResultado2;
        }

        protected void Grid_DataBinding(object sender, EventArgs e)
        {
            ((GridView)sender).Visible = true;
        }

        protected void Grid_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                DemandModel current = ((CDSBDemandControl.Model.DemandModel)(e.Row.DataItem));

                if (!current.Conclude.HasValue)
                {
                    e.Row.Cells[4].Text = "informar data";
                    e.Row.Cells[4].BackColor = Color.Red;
                }
            }

        }

        #region IDiscriminatorDemand Members

        void IDiscriminatorDemand.ExibirFila(IEnumerable<CDSBDemandControl.Model.DemandModel> demandModels)
        {
            throw new NotImplementedException();
        }

        void IDiscriminatorDemand.ExibirEsteira(IEnumerable<CDSBDemandControl.Model.DemandModel> demandModels)
        {
            throw new NotImplementedException();
        }

        void IDiscriminatorDemand.ExibirRanking(IEnumerable<CDSBDemandControl.Model.DeveloperModel> developers, IEnumerable<CDSBDemandControl.Model.DeveloperModel> leaders, IEnumerable<CDSBDemandControl.Model.DeveloperModel> focalpoints)
        {
            throw new NotImplementedException();
        }

        void IDiscriminatorDemand.ExibirFaturamento(List<CDSBDemandControl.Model.BillingModel> billing, bool canSeeBilling)
        {
            throw new NotImplementedException();
        }

        void IDiscriminatorDemand.ExibirCodeReview(IEnumerable<CDSBDemandControl.Model.DemandModel> WorkItemsWithCodeReview, IEnumerable<CDSBDemandControl.Model.DemandModel> WorkItemsWithoutCodeReview)
        {
            throw new NotImplementedException();
        }

        void IDiscriminatorDemand.ExibirBugsdeProducao(IEnumerable<CDSBDemandControl.Model.DemandModel> demandModels)
        {
            throw new NotImplementedException();
        }

        void IDiscriminatorDemand.ExibirScenariosAndTasks(List<CDSBDemandControl.Model.ScenarioModel> scenarios)
        {
            throw new NotImplementedException();
        }

        void IDiscriminatorDemand.ExibirListaDisp(IEnumerable<CDSBDemandControl.Model.DemandModel> list, IEnumerable<CDSBDemandControl.Model.DeveloperModel> modelAusencias, List<DateTime> fer)
        {
            throw new NotImplementedException();
        }

        void IDiscriminatorDemand.ExibirBugsdeProducaoUltimoMes(List<CDSBDemandControl.Model.DemandModel> BugList)
        {
            throw new NotImplementedException();
        }

        void IDiscriminatorDemand.ExibirFactoryWaitingList(List<CDSBDemandControl.Model.BillingModel> Billing, bool AllowsShow)
        {
            throw new NotImplementedException();
        }

        void IDiscriminatorDemand.ExibirAlocacao(IEnumerable<CDSBDemandControl.Model.DemandModel> demandModels)
        {
            throw new NotImplementedException();
        }

        void IDiscriminatorDemand.ExibirFactoryProductionList(List<BillingModel> billing, bool canSeeBilling)
        {
            throw new NotImplementedException();
        }

        void IDiscriminatorDemand.ExibirSolicitacoesSites(List<CDSBDemandControl.Model.DemandModel> demandModels)
        {
            throw new NotImplementedException();
        }

        void IDiscriminatorDemand.ExibirManagementTeamTasks(List<CDSBDemandControl.Model.DemandModel> demandModels)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
